package cn.edu.njuit.children.apis;

import cn.edu.njuit.children.common.Result;
import cn.edu.njuit.children.entity.UserAccount;
import cn.edu.njuit.children.mapper.UserMapper;
import cn.edu.njuit.children.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: TODO
 * @Data: 2022/11/29 11:39
 * @Author: guzy
 */
@RestController
@RequestMapping("/api")
@Api(value = "用户接口", tags = "用户接口")
public class UserAccountApi {

    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;

    @ApiOperation(value = "获取用户个人信息详情",tags = "无需传参")
    @GetMapping("/userinfo")
    public Result getUserInfo() {
        //获取当前登录用户
        String phone = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        UserAccount account = userService.getAccount(phone);
        return Result.success(account);
    }

    @ApiOperation(value = "修改用户个人详情",tags = "id必填")
    @ApiImplicitParams(@ApiImplicitParam(name = "UserAccount", value = "userAccount实体类", required = true))
    @PutMapping("/userinfo")
    public Result putUserInfo(@RequestBody UserAccount userAccount) {
        int i = userMapper.updateById(userAccount);
        if (i>0){
            return Result.success("修改成功");
        }else {
            return Result.failure("修改失败");
        }
    }

    @ApiOperation("修改密码")
    @ApiImplicitParams(@ApiImplicitParam(name = "newPwd", value = "新密码", required = true))
    @PutMapping("/forgetPassword")
    public Result forgetPassword(@RequestParam String newPwd) {
        boolean b = userService.putPassword(newPwd);
        if (b){
            return Result.success("修改成功");
        }else {
            return Result.failure("修改失败");
        }
    }
}
